package fr.insee.rome.io.finder;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import fr.insee.rome.factory.RomeFactory;
import fr.insee.rome.io.mots.Mots;
import fr.insee.rome.string.comp.StringComparator;
import fr.insee.rome.string.norm.RomeNormalizer;

public class SlowFinder implements RomeFinder{

	private Mots mots;
	private RomeNormalizer normalizer;
	private StringComparator comparator;
	
	public SlowFinder(){
		this.mots = RomeFactory.getMots();
		this.normalizer = RomeFactory.getNormalizer();
	}
	
	@Override
	public Set<String> lookup(String libelle) throws IOException {
		
		Set<String> elements = normalizer.normaliser(libelle);
		Set<String> codes = new HashSet<String>();
		for (String element : elements) {
			if(mots.get(element) != null){
				codes.addAll(mots.get(element));
			}
			else{
				for (String mot : mots) {
					if(comparator.matches(mot, element)){
						codes.addAll(mots.get(mot));
					}
				}
			}
		}
		return codes;
	}

}
